package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.domain.OrderAddress;
import com.ruoyi.order.domain.app.Vo.*;
import com.ruoyi.order.domain.app.from.OrderDetailsResultVo;
import com.ruoyi.order.domain.app.from.OrderFrom;
import com.ruoyi.order.domain.app.from.OrderResultVo;
import com.ruoyi.order.domain.app.from.OrderVo;
import com.ruoyi.order.domain.app.from.cart.OrderConfirmFrom;
import com.ruoyi.order.service.IOrderAddressService;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.order.service.IShoppingCarService;
import com.ruoyi.order.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈焕然
 * @version 1.0
 */
@Controller
@RequestMapping("/app")
public class OrderAppController extends BaseController {
    @Autowired
    private IOrderAddressService orderAddressService;

    @Autowired
    private IShoppingCarService shoppingCarService;

    @Autowired
    private IOrderService orderService;
    /**
     * 未完成
     * @param order
     * @return
     */
    @RequestMapping("/p/order/submit")
    public OrderResultVo submitOrder(OrderFrom order){
        for (OrderFrom.OrderShopParam orderShopParam : order.getOrderShopParam()) {
            if(orderShopParam.getRemark()!=null){
                Order order1 = new Order();
                order1.setRemark(orderShopParam.getRemark());
                orderService.insertOrder(order1);
            }
        }
        OrderResultVo result = new OrderResultVo();

        return result;
    }

    /**
     * 未完成
     * @param
     * @return
     * ?current=1&size=10&status=1
     */
    @RequestMapping("/p/myOrder/myOrder")
    public OrderResultVo getMyOrder(Integer current,Integer size){



        return null;
    }
    //
    @RequestMapping("/p/order/confirm")
    @ResponseBody
    public OrderConfirmResultVo confirm(OrderConfirmFrom confirmFrom){
        OrderConfirmResultVo result = new OrderConfirmResultVo();
        List<ShopCartOrders> shopCartOrders = new ArrayList<>();
        shopCartOrders.add(new ShopCartOrders());
        List<ShopCartItemDiscount> shopCartItemDiscounts = new ArrayList<>();
        shopCartItemDiscounts.add(new ShopCartItemDiscount());
        if(confirmFrom!=null){
            UserAddr userAddr = new UserAddr();
            Integer[] basketIds = confirmFrom.getBasketIds();
            ShopMoneyVo totalPay = shoppingCarService.getTotalPay(basketIds);
            int count = shoppingCarService.getCount();
            BigDecimal totalMoneyMoney = BigDecimal.ZERO;
            if(totalPay!=null){
                totalMoneyMoney  = totalPay.getTotalMoneyMoney();
            }
            for (Integer basketId : basketIds) {
                userAddr  = shoppingCarService.getUserAddr(basketId);
            }
            if(userAddr!=null){
                result.setUserAddr(userAddr);
            }
            List<CartItem> shopCartItems  = shoppingCarService.getShopCartProductInfoByBasketIds(basketIds);
            if(shopCartItems!=null&&shopCartItems.size()>0){
                shopCartItemDiscounts.get(0).setShopCartItems(shopCartItems);
                shopCartItemDiscounts.get(0).setTotalAmount(totalMoneyMoney);
            }
            shopCartOrders.get(0).setShopCartItemDiscounts(shopCartItemDiscounts);
            shopCartOrders.get(0).setTotalCount(count);
            shopCartOrders.get(0).setActualTotal(totalMoneyMoney);
            shopCartOrders.get(0).setUseScore(10);
            shopCartOrders.get(0).setTotal(totalMoneyMoney);
        }

        return result;
    }

    //    GET ?=1491606556205387776 HTTP/1.1
    /**
     * 未完成
     * @param
     * @return
     * ?current=1&size=10&status=1
     */
    @RequestMapping("/p/myOrder/orderDetail")
    @ResponseBody
    public OrderDetailsResultViewVo getOrderDetail(String orderNumber){
        return orderService.getOrderInfoByOrderNO(orderNumber);
    }
    // ?orderNumbers=1491606556205387776
    @RequestMapping("/p/order/getOrderPayInfoByOrderNumber")
    @ResponseBody
    public OrderViewVo getOrderPayInfoByOrderNumber(String orderNumber){
        return orderService.getOrderPayInfoByOrderNumber(orderNumber);
    }
}
